<template>
  <div class="login">
      <!-- 中央的登录盒子 -->
      <div class="login_box">
        <!-- 头像区域 -->
        <div class="avatar">
            <img src="../assets/heima.png" alt="">
        </div>

        <!--
          表单区域 model属性用来管理表单的所有数据
          通过rules来定义校验规则
        -->
        <!-- 登陆表单区域 -->
        <el-form ref="loginForm" class="login_form" :model="loginForm" :rules="loginFormRules">
            <!-- 用户名 -->
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
            </el-form-item>
            <el-form-item class="bottom_btn">
                <el-button type="primary" @click="login">登陆</el-button>
                <el-button type="info" @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
      </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单数据
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单的验证规则
      loginFormRules: {
        // 数组代表可以写多个校验规则
        username: [
          // 如果没有值
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // 限制字符长度
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created () {

  },

  methods: {
    // 重置表单
    resetForm () {
      // ref: 就是原生dom 或者一个组件的引用
      this.$refs.loginForm.resetFields()
    },
    login () {
      this.$refs.loginForm.validate(async flag => {
        if (!flag) return
        // 解构赋值用冒号重命名
        const { data: res } = await this.$axios.post('/login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success('登录成功')
        // localStorage永久储存 sessionStorage关闭页面后消失
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang='less'>
.login{
  width: 100%;
  height: 100%;
  background: #2b4b6b;

  .login_box{
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 10px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);

    .avatar{
        width: 100px;
        height: 100px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0px 0px 10px #ddd;
        position: absolute;
        top: -50px;
        left: 50%;
        transform: translate(-50%);
        background: #fff;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
        }
    }

    .login_form{
        margin-top: 100px;
        padding: 0 20px;

        .bottom_btn{
            display: flex;
            justify-content: flex-end;
        }
    }
  }
}

</style>
